/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sack.picasawebalbums;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openide.util.Exceptions;

/**
 *
 * @author admin
 */
public class Album {
    String AlbumName;
    URL url;
    PicasawebService myService;
    AlbumFeed af;
    List<MediaThumbnail> thumbnailList;
    File imageFile;
    String thumbnailPath;
    private URL thumbnailUrl;
    static String path = java.lang.System.getProperty("user.home")+"/PicasaAlbums";
    private AlbumEntry myAlbumEntry;
    
    public Album(String name,Feeds service,AlbumEntry albumentry){
        AlbumName = name;
        myService = service.myService;
        myAlbumEntry = albumentry;
        init();
    }

    @Override
    public String toString(){
        return AlbumName;
    }
    public AlbumEntry getAlbumEntry(){
        return myAlbumEntry;
    }

    public File getAlbumImage(PicasaAlbumTopComponent aThis) {
        if(!getthumbnailList().isEmpty()){
            try {
                thumbnailUrl = new URL(getthumbnailList().get(0).getUrl());
            } catch (MalformedURLException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        imageFile = getImage(thumbnailUrl);
        return imageFile;
        
    }

    void setThumbnailPath(String absoluteFile) {
        thumbnailPath = absoluteFile;
    }

    private File getImage(URL imageURL) {
        InputStream inputStream = null;
        try {
            inputStream = imageURL.openStream();
            if (inputStream != null) {
                try {
                    imageFile = getLocalMachinePath();
                    setThumbnailPath(imageFile.getAbsolutePath());
                    FileOutputStream fos = new FileOutputStream(imageFile);
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) > 0) {
                        fos.write(buffer, 0, bytesRead);
                    }
                    fos.close();
              }catch (Exception ex) {
                    if (imageFile != null) {
                        if (imageFile.exists()) 
                            imageFile.delete();
                        imageFile = null;
                    }
                }
            }
            } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            try {
                inputStream.close();
            } 
            
            catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
            catch(NullPointerException ex){
                
            }
            return imageFile;
    }
    }

    private File getLocalMachinePath() {
        if(new File(path).exists()){
            return new File(path+"/"+AlbumName+"thumb.jpg");
        }else{
            return null;
        }
        
    }
    private void init() {
        thumbnailList = myAlbumEntry.getMediaThumbnails();
        //if the album is made
        if(!new File(path+"/"+AlbumName).exists()){
            new File(path+"/"+AlbumName).mkdir();
        }
    }
    public List<MediaThumbnail> getthumbnailList(){
        return thumbnailList;
    }
}